package com.logistic.app

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.util.Log
import androidx.core.content.FileProvider
import com.sunmi.printerx.PrinterSdk
import com.sunmi.printerx.api.PrintResult
import com.sunmi.printerx.style.BaseStyle
import com.sunmi.printerx.style.TextStyle
import io.flutter.plugin.common.BinaryMessenger
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import java.io.File


class PrinterChannel(flutterEngine: BinaryMessenger, context: Context): MethodChannel.MethodCallHandler {
    private val printerChannelName = "app.channel.shared.data"
    private var channel: MethodChannel
    private var mContext: Context

    companion object {
        private const val TAG = "PrinterChannel"
    }

    init {
        Log.d(TAG, "init")
        channel = MethodChannel(flutterEngine, printerChannelName)
        channel.setMethodCallHandler(this)
        mContext = context;
    }

    override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) {
        Log.d(TAG, "onMethodCall: " + call.method)
        if (call.method == "printerHandler_flow") {
            printLabel(call.arguments as Map<String, String>)
        }else if(call.method == "printInbound_flow"){
            printInbound(call.arguments as Map<String, String>)
        }else if(call.method == "printerTake_flow"){
            printTake(call.arguments as Map<String, String>)
        }else if(call.method == "restartApp"){
            restartApp();
        } else {
            result.notImplemented()
        }
    }

    /**
     * 重启app
     */
    private fun restartApp(){
        val packageName: String = mContext.getPackageName();
        var intent: Intent? = mContext.packageManager.getLaunchIntentForPackage(packageName);
        intent?.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK);
        mContext.startActivity(intent);
    }

    /**
     * 打印一张商品信息标签
     */
    private fun printLabel(arg: Map<String, String>) {
        var spacingY = 30
        var positionY = 0
        var isTitleWrap = false;
        Log.d(TAG, "arg: $arg")
        PrinterSdk.getInstance().getPrinter(mContext, object : PrinterSdk.PrinterListen {
            override fun onDefPrinter(printer: PrinterSdk.Printer?) {
                printer?.canvasApi()?.run {
                    initCanvas(BaseStyle.getStyle().setPosX(0).setPosY(0).setWidth(460).setHeight(320))
//                    renderArea(AreaStyle.getStyle().setStyle(Shape.BOX).setPosX(0).setPosY(20).setWidth(450).setHeight(315))
                    positionY = positionY.plus(20)
                    if (arg["wcName"] != null) {
                        if (arg["wcName"]!!.length > 8) {
                            isTitleWrap = true
                            renderText(arg["wcName"]?.substring(0, 8), TextStyle.getStyle().setTextSize(40).enableBold(true)
                                .setPosX(10).setPosY(positionY))
                            positionY = positionY.plus(50)
                            renderText(arg["wcName"]!!.substring(8), TextStyle.getStyle().setTextSize(40).enableBold(true)
                                .setPosX(10).setPosY(positionY))
                        } else {
                            renderText(arg["wcName"], TextStyle.getStyle().setTextSize(40).enableBold(true)
                                .setPosX(10).setPosY(positionY))
                        }
                    }

                    positionY = positionY.plus(50)
                    renderText("工作令: ", TextStyle.getStyle().setTextSize(20)
                        .setPosX(10).setPosY(positionY))
                    renderText(arg["workNo"], TextStyle.getStyle().setTextSize(20)
                        .setPosX(100).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("任务号: ", TextStyle.getStyle().setTextSize(20)
                        .setPosX(10).setPosY(positionY))
                    renderText(arg["shopOrder"], TextStyle.getStyle().setTextSize(20)
                        .setPosX(100).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("客户名称: ", TextStyle.getStyle().setTextSize(20)
                        .setPosX(10).setPosY(positionY))
                    if (arg["customerName"] != null){
                        if(isTitleWrap){
                            // title有换行就采取三个点取代
                            renderText(if (arg["customerName"]!!.length > 13) arg["customerName"]?.substring(0, 11)
                                    + "..." else arg["customerName"],
                                TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                            )
                        } else {
                            // title不换行就正常显示
                            if(arg["customerName"]!!.length <= 13){
                                renderText(
                                    arg["customerName"],
                                    TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                                )
                            }else {
                                renderText(
                                    arg["customerName"]!!.substring(0, 13),
                                    TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                                )
                                positionY = positionY.plus(spacingY)
                                renderText(
                                    arg["customerName"]!!.substring(13),
                                    TextStyle.getStyle().setTextSize(20)
                                        .setPosX(100).setPosY(positionY)
                                )
                            }
                        }
                    }

                    positionY = positionY.plus(spacingY)
                    renderText("后工序开工时间: ", TextStyle.getStyle().setTextSize(20)
                        .setPosX(10).setPosY(positionY))
                    renderText(arg["startDate"], TextStyle.getStyle().setTextSize(20)
                        .setPosX(160).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("图纸位置: ", TextStyle.getStyle().setTextSize(25)
                        .setPosX(10).setPosY(positionY))
                    renderText(arg["paintLocation"], TextStyle.getStyle().setTextSize(20)
                        .setPosX(100).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("交接数量: ", TextStyle.getStyle().setTextSize(20)
                        .setPosX(10).setPosY(positionY))
                    renderText(arg["orderQty"], TextStyle.getStyle().setTextSize(20)
                        .setPosX(100).setPosY(positionY))
                    renderText("需求数量: ", TextStyle.getStyle().setTextSize(20)
                        .setPosX(150).setPosY(positionY))
                    renderText(arg["planQty"], TextStyle.getStyle().setTextSize(20)
                        .setPosX(240).setPosY(positionY))

                    printCanvas(1, object : PrintResult() {
                        override fun onResult(resultCode: Int, message: String?) {
                            if(resultCode == 0) {
                                //打印完成
                            } else {
                                //打印失败
                                println(printer.queryApi()?.status)
                            }
                        }
                    })
                }
            }

            override fun onPrinters(printers: MutableList<PrinterSdk.Printer>?) {
                println(printers)
            }
        });
    }

    /**
     * 打印一张商品信息标签
     */
    private fun printInbound(arg: Map<String, String>) {
        var spacingY = 30
        var positionY = 0
        var isTitleWrap = false;
        Log.d(TAG, "arg: $arg")
        PrinterSdk.getInstance().getPrinter(mContext, object : PrinterSdk.PrinterListen {
            override fun onDefPrinter(printer: PrinterSdk.Printer?) {
                printer?.canvasApi()?.run {
                    initCanvas(BaseStyle.getStyle().setPosX(0).setPosY(0).setWidth(460).setHeight(320))
                    positionY = positionY.plus(20)
                    if (arg["endPoint"] != null) {
                        if (arg["endPoint"]!!.length > 8) {
                            isTitleWrap = true
                            renderText(arg["endPoint"]?.substring(0, 8), TextStyle.getStyle().setTextSize(40).enableBold(true)
                                    .setPosX(10).setPosY(positionY))
                            positionY = positionY.plus(50)
                            renderText(arg["endPoint"]!!.substring(8), TextStyle.getStyle().setTextSize(40).enableBold(true)
                                    .setPosX(10).setPosY(positionY))
                        } else {
                            renderText(arg["endPoint"], TextStyle.getStyle().setTextSize(40).enableBold(true)
                                    .setPosX(10).setPosY(positionY))
                        }
                    }
                    positionY = positionY.plus(50)
                    renderText("工作令: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["workNo"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(100).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("流向任务号: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["parentOrderNo"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(160).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("客户名称: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    if (arg["customerName"] != null){
                        if(isTitleWrap){
                            // title有换行就采取三个点取代
                            renderText(if (arg["customerName"]!!.length > 13) arg["customerName"]?.substring(0, 11)
                                    + "..." else arg["customerName"],
                                    TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                            )
                        } else {
                            // title不换行就正常显示
                            if(arg["customerName"]!!.length <= 13){
                                renderText(
                                        arg["customerName"],
                                        TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                                )
                            }else {
                                renderText(
                                        arg["customerName"]!!.substring(0, 13),
                                        TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                                )
                                positionY = positionY.plus(spacingY)
                                renderText(
                                        arg["customerName"]!!.substring(13),
                                        TextStyle.getStyle().setTextSize(20)
                                                .setPosX(100).setPosY(positionY)
                                )
                            }
                        }
                    }

                    positionY = positionY.plus(spacingY)
                    renderText("下序开工时间: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["startDate"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(160).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("工序部门: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["nextDeptNameWc"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(100).setPosY(positionY))
                    positionY = positionY.plus(spacingY)
                    renderText("数量: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["orderQty"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(100).setPosY(positionY))

                    printCanvas(1, object : PrintResult() {
                        override fun onResult(resultCode: Int, message: String?) {
                            if(resultCode == 0) {
                                //打印完成
                            } else {
                                //打印失败
                                println(printer.queryApi()?.status)
                            }
                        }
                    })
                }
            }

            override fun onPrinters(printers: MutableList<PrinterSdk.Printer>?) {
                println(printers)
            }
        });
    }

    /**
     * 工序周转打印分支
     */
    private fun printTake(arg: Map<String, String>) {
        var spacingY = 30
        var positionY = 0
        var isTitleWrap = false;
        Log.d(TAG, "arg: $arg")
        PrinterSdk.getInstance().getPrinter(mContext, object : PrinterSdk.PrinterListen {
            override fun onDefPrinter(printer: PrinterSdk.Printer?) {
                printer?.canvasApi()?.run {
                    initCanvas(BaseStyle.getStyle().setPosX(0).setPosY(0).setWidth(460).setHeight(320))
//                    renderArea(AreaStyle.getStyle().setStyle(Shape.BOX).setPosX(0).setPosY(20).setWidth(450).setHeight(315))
                    positionY = positionY.plus(20)
                    if (arg["wcName"] != null) {
                        if (arg["wcName"]!!.length > 8) {
                            isTitleWrap = true
                            renderText(arg["wcName"]?.substring(0, 8), TextStyle.getStyle().setTextSize(40).enableBold(true)
                                    .setPosX(10).setPosY(positionY))
                            positionY = positionY.plus(50)
                            renderText(arg["wcName"]!!.substring(8), TextStyle.getStyle().setTextSize(40).enableBold(true)
                                    .setPosX(10).setPosY(positionY))
                        } else {
                            renderText(arg["wcName"], TextStyle.getStyle().setTextSize(40).enableBold(true)
                                    .setPosX(10).setPosY(positionY))
                        }
                    }

                    positionY = positionY.plus(50)
                    renderText("工作令: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["workNo"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(100).setPosY(positionY))

                    positionY = positionY.plus(spacingY)
                    renderText("任务号: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["shopOrder"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(100).setPosY(positionY))
                    if (arg["customerName"] != null){
                        if(isTitleWrap){
                            // title有换行就采取三个点取代
                            renderText(if (arg["customerName"]!!.length > 13) arg["customerName"]?.substring(0, 11)
                                    + "..." else arg["customerName"],
                                    TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                            )
                        } else {
                            // title不换行就正常显示
                            if(arg["customerName"]!!.length <= 13){
                                renderText(
                                        arg["customerName"],
                                        TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                                )
                            }else {
                                renderText(
                                        arg["customerName"]!!.substring(0, 13),
                                        TextStyle.getStyle().setTextSize(20).setPosX(100).setPosY(positionY)
                                )
                                positionY = positionY.plus(spacingY)
                                renderText(
                                        arg["customerName"]!!.substring(13),
                                        TextStyle.getStyle().setTextSize(20)
                                                .setPosX(100).setPosY(positionY)
                                )
                            }
                        }
                    }

                    positionY = positionY.plus(spacingY)
                    renderText("后工序开工时间: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["startDate"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(160).setPosY(positionY))
                    positionY = positionY.plus(spacingY)
                    renderText("交接数量: ", TextStyle.getStyle().setTextSize(20)
                            .setPosX(10).setPosY(positionY))
                    renderText(arg["orderQty"], TextStyle.getStyle().setTextSize(20)
                            .setPosX(100).setPosY(positionY))
                    printCanvas(1, object : PrintResult() {
                        override fun onResult(resultCode: Int, message: String?) {
                            if(resultCode == 0) {
                                //打印完成
                            } else {
                                //打印失败
                                println(printer.queryApi()?.status)
                            }
                        }
                    })
                }
            }

            override fun onPrinters(printers: MutableList<PrinterSdk.Printer>?) {
                println(printers)
            }
        });
    }


}